package service

import (
	"errors"
	context3 "ksd-social-api/modules/msg/model/context"
	"ksd-social-api/modules/msg/service"
	"ksd-social-api/modules/resume/mapper"
	"ksd-social-api/modules/resume/model"
	"ksd-social-api/modules/resume/model/context"
	"ksd-social-api/modules/resume/model/vo"
	umapper "ksd-social-api/modules/user/mapper"
	umodel "ksd-social-api/modules/user/model"
	"ksd-social-api/utils"
)

type ResumeService struct{}

var adminResumeMapper = mapper.AdminResumeMapper{}
var adminResumeTemplateMapper = mapper.AdminResumeTemplateMapper{}
var resumeMapper = mapper.ResumeMapper{}
var messageMeService = service.MessageMeService{}
var userBuyVipMapper = umapper.UserBuyVipMapper{}
var userVipMapper = umapper.UserVipMapper{}
var userMapper = umapper.UserMapper{}
var resumeCodeMapper = mapper.ResumeCodeMapper{}
var resumeTemplateMapper = mapper.ResumeTemplateMapper{}
var resumeTemplateCodeMapper = mapper.ResumeTemplateCodeMapper{}
var userBuyResumeTemplateMapper = umapper.UserBuyResumeTemplateMapper{}

/**
 * @desc 查询用户简历
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (service *ResumeService) FindResumeByUserId(userId uint64, systemId uint64) []*vo.ResumeVo {
	resumesList := resumeMapper.FindResumeByUserId(userId, systemId)
	resumeVos := []*vo.ResumeVo{}
	for _, resume := range resumesList {
		resumeVo := vo.ResumeVo{}
		utils.CopyProperties(&resumeVo, resume)
		resumeVo.ResumeNewFile = ""
		resumeVos = append(resumeVos, &resumeVo)
	}
	return resumeVos
}

/*
*
保存简历
*/
func (service *ResumeService) SaveUpdateResume(ctx *context.ResumeContext) (*vo.ResumeVo, error) {
	var resume model.Resume
	utils.CopyProperties(&resume, ctx)
	count := resumeMapper.CountByUserId(ctx.UserId, ctx.SystemId)
	if count > 0 {
		return nil, errors.New("您已经添加过简历信息")
	}
	userVipVo := userVipMapper.GetUserVipDetail(8, ctx.SystemId)
	userVo := userMapper.GetUserByID(ctx.UserId, ctx.SystemId)
	var status int
	if userVo.VipFlag == 1 {
		status = 1
	} else {
		status = 2
	}
	resume.Price = userVipVo.Price
	resume.RType = 8
	resume.Status = status
	id := resumeMapper.SaveUpdateResume(resume)
	resume.Id = id
	resumeVo := vo.ResumeVo{}
	utils.CopyProperties(&resumeVo, resume)
	return &resumeVo, nil
}

/**
 * @author feige
 * @date 2023-10-08
 */
func (service *ResumeService) GetResumeId(resumeId uint64, systemId uint64) (pvo *vo.ResumeVo, err error) {
	// 1: 根据id查询数据层
	resume, err := resumeMapper.GetById(resumeId, systemId)
	if err != nil {
		return nil, err
	}
	resumeVo := vo.ResumeVo{}
	utils.CopyProperties(&resumeVo, resume)
	return &resumeVo, nil
}

/**
 * @author feige
 * @date 2023-10-08
 */
func (service *ResumeService) GetResumeFile(resumeId uint64, userId uint64, systemId uint64) (string, error) {
	// 1: 根据id查询数据层
	resume, err := resumeMapper.GetByIdAndUesrId(resumeId, userId, systemId)
	if err != nil {
		return "", err
	}
	if resume.Status < 2 {
		return "", errors.New("没有支付")
	}
	return resume.ResumeNewFile, nil
}

/**
 * @desc 更新状态
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (service *ResumeService) UpdateStatus(ctx *context.ResumeDetailContext) bool {
	return resumeMapper.UpdateStatus(ctx.Id, ctx.Status, ctx.SystemId)
}

/**
 * @desc 更新文件
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (service *ResumeService) UpdateResumeNewFile(ctx *context.ResumeDetailContext) bool {
	return resumeMapper.UpdateResumeNewFile(ctx.Id, ctx.Rfile, ctx.SystemId)
}

/**
 * @desc 更新状态
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (service *ResumeService) UpdateResumePay(id uint64, payMethod int, PayMethodName string, tradeNo string, userId uint64, systemId uint64) bool {
	pay := resumeMapper.UpdateResumePay(id, PayMethodName, tradeNo, systemId)
	if pay {
		pvo, _ := service.GetResumeId(id, systemId)
		userVipVo := userVipMapper.GetUserVipDetail(pvo.RType, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		var userBuyVip umodel.UserBuyVip
		userBuyVip.SystemId = systemId
		// 处理返回结果
		userBuyVip.VipId = pvo.RType
		userBuyVip.Title = userVipVo.Title
		userBuyVip.Description = userVipVo.Note

		userBuyVip.UserId = userId
		userBuyVip.Username = userVo.Username
		userBuyVip.Avatar = userVo.Avatar
		userBuyVip.Uuid = userVo.Uuid
		userBuyVip.Phone = userVo.Telephone
		userBuyVip.Address = userVo.Address
		userBuyVip.Nickname = userVo.Nickname

		userBuyVip.Code = ""
		userBuyVip.PayMethod = payMethod
		userBuyVip.PayMethodName = PayMethodName
		userBuyVip.Tradeno = tradeNo
		userBuyVip.Orderno = tradeNo
		userBuyVip.OrderJson = ""
		userBuyVip.Price = userVipVo.Price
		// 保存订单的接口
		userBuyVipMapper.SaveUserBuyVip(userBuyVip)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = pvo.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		messageMeContext.Url = "https://www.study512.com/resume"
		messageMeService.SaveMessageMeBuyResume(&messageMeContext)
	}
	return true
}
